package com.hncj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hncj.dao.UserMapper;
import com.hncj.pojo.User;
import com.hncj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public int getByUsername(User user) {

        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();

        userWrapper.eq(User::getUsername,user.getUsername());

        User user1 = userMapper.selectOne(userWrapper);

        if(user1 == null) return -1;

        else if(user1.getPassword().equals(user.getPassword())) return user1.getId();

        return -2;

    }

    @Override
    @Transactional
    public int setByUser(User user) {

        userMapper.insert(user);

        return getByUsername(user);

    }

}
